from selenium.webdriver.common.by import By
import time
from common.Utils import TestDriver
import datetime


#测试注册界面
class Register:
    url = ""
    driver = ""
    def __init__(self):
        self.url="http://127.0.0.1:58080/sign-up.html"
        self.driver = TestDriver.driver
        self.driver.get(self.url)

    def RegisterSucTest(self):
        self.driver.implicitly_wait(3)
        testname = datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #注册后登录,看如否能够进入主页
        self.driver.find_element(By.CSS_SELECTOR,"#nav_board_index > a > span.nav-link-title")

    def RegisterFailTest(self):
        self.driver.implicitly_wait(3)
        self.driver.get("http://127.0.0.1:58080/sign-up.html")
        testname = datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")

        #用户名-#signUpForm > div > div:nth-child(2) > div
        #昵称-#signUpForm > div > div:nth-child(3) > div
        #密码-#signUpForm > div > div:nth-child(4) > div > div
        #确认密码-# signUpForm > div > div:nth-child(5) > div > div

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(3) > div")
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(4) > div > div")
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(5) > div > div")
        self.driver.refresh()

        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(2) > div")
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(5) > div > div")
        self.driver.refresh()

        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(2) > div")
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(3) > div")
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(5) > div > div")
        self.driver.refresh()

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(2) > div")
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(3) > div")
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(4) > div > div")
        self.driver.refresh()

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(3) > div")
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(4) > div > div")
        self.driver.refresh()

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div")
        self.driver.refresh()

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys(testname)
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(5) > div > div")
        self.driver.refresh()

    def Registersecshow(self):
        self.driver.implicitly_wait(3)
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#password_a > svg").click()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat_a > svg").click()
        TestDriver.getScreeShot()
        self.driver.refresh()

    def RegistertoLogin(self):
        self.driver.implicitly_wait(3)
        self.driver.find_element(By.CSS_SELECTOR,"body > div > div > div.text-center.text-muted.mt-3 > a").click()
        actual = self.driver.find_element(By.CSS_SELECTOR,"body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a").text
        assert actual == "点击注册"



register=Register()
register.RegisterSucTest()
register.RegisterFailTest()
register.Registersecshow()
register.RegistertoLogin()

